package com.mrxu.monitor.base;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class ResultObj<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    public final static ResultObj<Object> OK = new ResultObj<Object> (ResultCode.OK);
    public final static ResultObj<Object> ERROR = new ResultObj<Object>(ResultCode.ERROR);

    private int code = ResultCode.OK.getCode();

    private String msg = ResultCode.OK.getMsg();

    private T result;

    public ResultObj(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultObj(int code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public ResultObj(ResultCode ResultCode) {
        this.code = ResultCode.getCode();
        this.msg = ResultCode.getMsg();
    }

    public ResultObj(ResultCode ResultCode, T result) {
        this.code = ResultCode.getCode();
        this.msg = ResultCode.getMsg();
        this.result = result;
    }

    /**
     * 通用的 操作成功
     */
    public static ResultObj<Object> success() {
        return new ResultObj<Object>(ResultCode.OK);
    }

    /**
     * 通用的 操作成功时的 返回对象
     */
    public static <T> ResultObj<T> success(T result) {
        ResultObj<T> ResultObj = new ResultObj<>(ResultCode.OK);
        ResultObj.result = result;
        return ResultObj;
    }

    /**
     * 通用的错误提示方法
     *
     * @param ResultCode 错误码
     */
    public static ResultObj<Object> error(ResultCode ResultCode) {
        return error(ResultCode.msg, ResultCode.code);
    }

    /**
     * 通用的错误提示方法
     *
     * @param msg 错误信息
     */
    public static ResultObj<Object> error(String msg) {
        return new ResultObj<Object>(ResultCode.ERROR.code, msg);
    }

    /**
     * 通用的错误提示方法
     *
     * @param msg     错误信息
     * @param code 错误码
     */
    public static ResultObj<Object> error(String msg, int code) {
        return new ResultObj<Object>(code, msg);
    }

    /**
     * 带错误数据的错误提示方法
     *
     * @param msg     错误信息
     * @param code 错误码
     * @param result  错误数据
     */
    public static <T> ResultObj<T> error(String msg, int code, T result) {
        ResultObj<T> ResultObj = new ResultObj<>(code, msg);
        ResultObj.result = result;
        return ResultObj;
    }

}

